package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.News;
import com.hengxinguotong.zhihuichengjian.bean.NewsInfo;
import com.hengxinguotong.zhihuichengjian.utils.Utils;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

/**
 * Created by Administrator on 2017/6/21.
 */

public class NewsListAdapter extends BaseAdapter {
    private Context context;
    private List<News> newsList;

    public NewsListAdapter(Context context, List<News> newsList) {
        this.context = context;
        this.newsList = newsList;
    }

    @Override
    public int getCount() {
        return newsList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(null == convertView || null == convertView.getTag()){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_news, parent, false);
            holder.news_pic = (ImageView)convertView.findViewById(R.id.news_pic);
            holder.news_title = (HXTextView)convertView.findViewById(R.id.news_title);
            holder.news_content = (HXTextView)convertView.findViewById(R.id.news_content);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }
        News news = newsList.get(position);
        String path = news.getPath();
        if(!Utils.isEmpty(path)){
            path = path.substring(0, path.lastIndexOf(".")) + "_100x100" + path.substring(path.lastIndexOf("."));
        }
        Glide.with(context)
                .load(path)
                .error(R.mipmap.load_failed)
                .into(holder.news_pic);
        holder.news_title.setText(news.getTitle());
        holder.news_content.setText(news.getContent());
        return convertView;
    }

    private class ViewHolder{
        private ImageView news_pic;
        private HXTextView news_title,news_content;
    }
}
